import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import { getUserInfoAPI } from '@/api/user'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '',
    userInfo: {}
  },
  getters: {
    username: state => state.userInfo.username,
    nickname: state => state.userInfo.nickname,
    user_pic: state => state.userInfo.user_pic
  },
  mutations: {
    updateToken (state, newvalue) {
      state.token = newvalue
    },
    updateuserInfo (state, newvalue) {
      state.userInfo = newvalue
    }
  },
  actions: {
    async getUserInfoAPIActions (store) {
      const { data: res } = await getUserInfoAPI()
      store.commit('updateuserInfo', res.data)
    }
  },
  modules: {
  },
  plugins: [createPersistedState()]
})
